<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BAPriceAdjust
 *
 * @author jianfeng
 */
class BAPriceAdjust {

    const Price_AdjustMode_Add_Percent = '+percent';
    const Price_AdjustMode_Less_Percent = '-percent';
    const Price_AdjustMode_Add_Yuan = '+yuan';
    const Price_AdjustMode_Less_Yuan = '-yuan';

    //put your code here
    public static function getFinalPrice($oldPrice, $adjustAmount, $adjustMode) {
        $price = $oldPrice;

        if ($adjustMode == self::Price_AdjustMode_Add_Percent) {
            $price = $price * (1 + $adjustAmount * 0.01);
        }
        if ($adjustMode == self::Price_AdjustMode_Less_Percent) {
            $price = $price * (1 - $adjustAmount * 0.01);
        }
        if ($adjustMode == self::Price_AdjustMode_Add_Yuan) {
            $price = $price + $adjustAmount;
        }
        if ($adjustMode == self::Price_AdjustMode_Less_Yuan) {
            $price = $price - $adjustAmount;
        }

        return $price;
    }

    public static function getAdjustModeList($region = 'china') {
        $list = array();
        if ($region == 'china') {
            $list[self::Price_AdjustMode_Less_Percent] = '减少百分比';
            $list[self::Price_AdjustMode_Less_Yuan] = '减少人民币元';
            $list[self::Price_AdjustMode_Add_Percent] = '增加百分比';
            $list[self::Price_AdjustMode_Add_Yuan] = '增加人民币元';
        }
        return $list;
    }

    public static function getAdjustModeAlias($adjustMode, $region = 'china') {
        $list = self::getAdjustModeList($region);
        return $list[$adjustMode];
    }

    public static function getUserPrice($bookableId, $data = NULL) {
        $userRoles = BRole::findUserRoles(NULL, array('spaceId' => Blyn::app()->getActiveSpace()->getId()));

        $spaceEngine = Blyn::app()->getActiveSpace()->getSpaceEngine();
        $space = Blyn::app()->getActiveSpace();

        $roleList = array();
        $roleList += $userRoles;
        foreach ($userRoles as $id => $role) {
            if ($role instanceof BRole) {
                $fullName = $role->getFullName();
                if (strpos($fullName, 'app_root.app_admin') !== FALSE || strpos($fullName, 'app_root.app_member') !== FALSE) {//admin or member role
                    $children = $role->getChildren(NULL, TRUE);
                    $roleList+= $children;
                }

                if (strpos($fullName, 'app_root.app_customer') !== FALSE) { //customer role
                    $parents = $role->getParent(TRUE);
                    foreach ($parents as $id => $role) {
                        if ($role->getName() != 'root' && $role->getName() != 'app_root')
                            $roleList[$id] = $role;
                    }
                }
            }
        }

        $roleIdList = array();
        foreach ($roleList as $id => $role) {
            $roleIdList[$id] = $id;
        }

        $finalAdjustTable = new BASheet('finalPriceAdjust');

        $finalAdjustTable->setOwner(NULL);

        $priceView = $finalAdjustTable->getView(array('itemData' => array(
                'bookableId' => $bookableId,
                'roleId' => $roleIdList),
            'spaceId' => $space->getId()
        ));

        $pList = array();
        $minPrice = array();
        $maxPrice = array();
        foreach ($priceView as $id => $list) {
            $price = $list->getItem('finalPrice')->getValue();
            $roleName = $list->getItem('roleName')->getValue();
            $roleFullName = $list->getItem('roleFullName')->getValue();
            $priceName = $list->getItem('priceAdjustName')->getValue();

            if (!isset($minPrice['price'])) {
                $minPrice['price'] = $price;
                $minPrice['roleName'] = $roleName;
                $minPrice['roleFullName'] = $roleFullName;
                $minPrice['priceName'] = $priceName;
            } elseif ($minPrice['price'] > $price) {
                $minPrice['price'] = $price;
                $minPrice['roleName'] = $roleName;
                $minPrice['roleFullName'] = $roleFullName;
                $minPrice['priceName'] = $priceName;
            }

            if (!isset($maxPrice['price'])) {
                $maxPrice['price'] = $price;
                $maxPrice['roleName'] = $roleName;
                $maxPrice['roleFullName'] = $roleFullName;
                $maxPrice['priceName'] = $priceName;
            } elseif ($maxPrice['price'] < $price) {
                $maxPrice['price'] = $price;
                $maxPrice['roleName'] = $roleName;
                $maxPrice['roleFullName'] = $roleFullName;
                $maxPrice['priceName'] = $priceName;
            }
            $pList['maxPrice'] = $maxPrice;
            $pList['minPrice'] = $minPrice;
        }


        return $pList;
    }

}
